package com.hotel.hotelsystem.controller;



import com.common.status.ResultMsg;
import com.common.status.StatusCode;
import com.common.utils.ResultUtils;
import com.common.utils.ResultVo;
import com.hotel.hotelsystem.pojo.TenantList;
import com.hotel.hotelsystem.service.TenantListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Api(value = "TenantListController",tags = {"入住人模块"})
@RestController
@RequestMapping("tenant")
public class TenantListController {

    @Resource
    private TenantListService tenantListService;

    @ApiOperation(value = "保存入住人信息")
    @PostMapping("/saveTenantList")
    public ResultVo<List<TenantList>> saveTenantList(String[] tenantName, Long[] tenantTelno, Long[] idCard, int orderId){

//        TenantList tenant = new TenantList();
//        tenant.setTenantName(tenantName);
//        tenant.setTenantTelno(tenantTelno);
//        tenant.setTenantIdcard(idCard);
//        tenant.setOrderId(orderId);
//        tenant.setCreateTime(LocalDateTime.now());
//        tenant.setUpdateTime(LocalDateTime.now());
//        tenant.setVersion(1);
//        tenant.setState(1);
//        boolean result = tenantListService.save(tenant);

        List<TenantList> list = new ArrayList<>();
        boolean result = true;
        for(int i=0;i<tenantName.length;i++){
            TenantList tenant = new TenantList();
            tenant.setTenantName(tenantName[i]);
            tenant.setTenantTelno(tenantTelno[i]);
            tenant.setTenantIdcard(idCard[i]);
            tenant.setOrderId(orderId);
            list.add(tenant);
            result=result&&tenantListService.save(tenant);
        }


        if (result) return ResultUtils.success(ResultMsg.SUCCESS_MSG, StatusCode.SUCCESS_CODE,result);
        else return ResultUtils.error(ResultMsg.DATA_NULL, StatusCode.SYSTEM_ERROR_CODE,null);
    }

    @ApiOperation(value = "根据订单id查看入住人信息")
    @GetMapping("/getTenantListByOrderId/{orderId}")
    public ResultVo<List<TenantList>> getTenantListById( @PathVariable("orderId") int orderId){
        Map<String,Object> map = new HashMap<>();
        map.put("order_id",orderId);
        List<TenantList> list = tenantListService.listByMap(map);

        if (list.size() != 0) return ResultUtils.success(ResultMsg.SUCCESS_MSG, StatusCode.SUCCESS_CODE,list);
        else return ResultUtils.error(ResultMsg.DATA_NULL, StatusCode.SYSTEM_ERROR_CODE,null);
    }

    @ApiOperation(value = "查看全部入住人信息")
    @GetMapping("/getTenantList")
    public ResultVo<List<TenantList>> getTenantList(){
        List<TenantList> list = tenantListService.list(null);

        if (list.size() != 0) return ResultUtils.success(ResultMsg.SUCCESS_MSG, StatusCode.SUCCESS_CODE,list);
        else return ResultUtils.error(ResultMsg.DATA_NULL, StatusCode.SYSTEM_ERROR_CODE,null);
    }
}
